/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Thibault
 */
@Entity
@Table(name = "customerorder_book", catalog = "bookshopdb", schema = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "CustomerorderBook.findAll", query = "SELECT c FROM CustomerorderBook c"),
    @NamedQuery(name = "CustomerorderBook.findByCustomerOrderid", query = "SELECT c FROM CustomerorderBook c WHERE c.customerorderBookPK.customerOrderid = :customerOrderid"),
    @NamedQuery(name = "CustomerorderBook.findByBookid", query = "SELECT c FROM CustomerorderBook c WHERE c.customerorderBookPK.bookid = :bookid"),
    @NamedQuery(name = "CustomerorderBook.findByQuantity", query = "SELECT c FROM CustomerorderBook c WHERE c.quantity = :quantity"),
    @NamedQuery(name = "CustomerorderBook.itemNumber", query = "SELECT SUM(c.quantity) FROM CustomerorderBook c WHERE c.customerorderBookPK = :customerorderBookPK")})
    //@NamedQuery(name = "CustomerorderBook.items", query = "SELECT c FROM CustomerorderBook c WHERE c.customerOrderid = :customerOrderid")})

public class CustomerorderBook implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected CustomerorderBookPK customerorderBookPK;
    @Basic(optional = false)
    @NotNull
    @Column(nullable = false)
    private int quantity;
    @JoinColumn(name = "CustomerOrder_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Customerorder customerorder;
    @JoinColumn(name = "Book_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Book book;

    public CustomerorderBook() {
    }

    public CustomerorderBook(Book book, int quantity) {
        this.quantity = quantity;
        this.book = book;
    }

    public CustomerorderBook(CustomerorderBookPK customerorderBookPK) {
        this.customerorderBookPK = customerorderBookPK;
    }

    public CustomerorderBook(CustomerorderBookPK customerorderBookPK, int quantity) {
        this.customerorderBookPK = customerorderBookPK;
        this.quantity = quantity;
    }

    public CustomerorderBook(int customerOrderid, int bookid) {
        this.customerorderBookPK = new CustomerorderBookPK(customerOrderid, bookid);
    }

    public CustomerorderBookPK getCustomerorderBookPK() {
        return customerorderBookPK;
    }

    public void setCustomerorderBookPK(CustomerorderBookPK customerorderBookPK) {
        this.customerorderBookPK = customerorderBookPK;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Customerorder getCustomerorder() {
        return customerorder;
    }

    public void setCustomerorder(Customerorder customerorder) {
        this.customerorder = customerorder;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (customerorderBookPK != null ? customerorderBookPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CustomerorderBook)) {
            return false;
        }
        CustomerorderBook other = (CustomerorderBook) object;
        if ((this.customerorderBookPK == null && other.customerorderBookPK != null) || (this.customerorderBookPK != null && !this.customerorderBookPK.equals(other.customerorderBookPK))) {
            return false;
        }
        return true;
    }

   
}
